<?php
namespace Hilaoyu\ComputeCloudClient;

use Hilaoyu\ComputeCloudClient\Api\HuaWeiEcs;
use Hilaoyu\ComputeCloudClient\Api\HuaWeiVpc;
use Hilaoyu\ComputeCloudClient\Interfaces\ComputeCloudClientInterface;
use Hilaoyu\Utils\Traits\ErrorsTrait;
use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
use HuaweiCloud\SDK\Core\ClientBuilder;
use HuaweiCloud\SDK\Core\Http\HttpConfig;
use HuaweiCloud\SDK\Ecs\V2\EcsClient;
use HuaweiCloud\SDK\Evs\V2\EvsClient;
use HuaweiCloud\SDK\Ims\V2\ImsClient;
use IPTools\Network;


class HuaWeiCloudApi implements ComputeCloudClientInterface
{
    use ErrorsTrait;

    protected $accessKeyId;
    protected $accessKeySecret;
    protected $projectId;

    static public function make($accessKeyId, $accessKeySecret,$projectId=null){
        return new static($accessKeyId, $accessKeySecret,$projectId);
    }

    public function __construct($accessKeyId, $accessKeySecret,$projectId=null){
        $this->accessKeyId = $accessKeyId;
        $this->accessKeySecret = $accessKeySecret;
        $this->projectId = $projectId;
    }



    public function buildClient($clientType,$endpoint){
        $credentials = new BasicCredentials($this->accessKeyId,$this->accessKeySecret,$this->projectId);
        $config = HttpConfig::getDefaultConfig();
        $config->setIgnoreSslVerification(true);
        $client = (new ClientBuilder($clientType))
            ->withHttpConfig($config)
            ->withEndpoint($endpoint)
            ->withCredentials($credentials)
            ->build();
        return $client;
    }


    /**
     * @param string $regionId
     * @return HuaWeiVpc
     */
    public function vpc($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://vpc.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://vpc.myhuaweicloud.com';
        }

        $client = $this->buildClient(new \HuaweiCloud\SDK\Vpc\V2\VpcClient(),$endpoint);

        return new HuaWeiVpc($client,$this,$regionId);

    }
    public function vpcV2($regionId=''){
        return $this->vpc($regionId);

    }
    public function vpcV3($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://vpc.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://vpc.myhuaweicloud.com';
        }

        $client = $this->buildClient(new \HuaweiCloud\SDK\Vpc\V3\VpcClient(),$endpoint);

        return new HuaWeiVpc($client,$this,$regionId);

    }
    public function eip($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://vpc.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://vpc.myhuaweicloud.com';
        }

        $client = $this->buildClient( new \HuaweiCloud\SDK\Eip\V2\EipClient(),$endpoint);

        return new HuaWeiVpc($client,$this,$regionId);

    }

    public function cbn($regionId=''){
        return null;

    }

    public function ecs($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://ecs.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://ecs.myhuaweicloud.com';
        }
        $client = $this->buildClient(new EcsClient(),$endpoint);

        return new HuaWeiEcs($client,$this,$regionId);

    }
    public function ims($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://ims.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://ims.myhuaweicloud.com';
        }
        $client = $this->buildClient(new ImsClient(),$endpoint);

        return new HuaWeiEcs($client,$this,$regionId);

    }
    public function evs($regionId=''){
        $regionId = trim($regionId);

        if($regionId){
            $endpoint = 'https://evs.'.$regionId.'.myhuaweicloud.com';
        }else{
            $endpoint = 'https://evs.myhuaweicloud.com';
        }
        $client = $this->buildClient(new EvsClient(),$endpoint);

        return new HuaWeiEcs($client,$this,$regionId);

    }



    static public function GetGateWay(string $network) :string{
        try{
            $network = Network::parse($network);
            //return (string)$network->getLastIP()->prev();
            return (string)$network->getFirstIP()->next();
        }catch (\Exception $exception){
            return "";
        }
    }
}
